// pages/account/regist/regist.js
const app = getApp();
var http = require('../../../utils/util.js')
import WxValidate from '../../../utils/WxValidate.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    form: {
      phone: '',
      code: '',
    },
    codeBtnText: '获取验证码',
    btnStatus: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();
  },

  backBtn() {
    wx.navigateBack({
      delta: 1
    });
  },

  /**
   * 单项绑定form数据
   */
  phoneInput(e) {
    this.setData({
      'form.phone': e.detail.value
    })
  },
  codeInput(e) {
    this.setData({
      'form.code': e.detail.value
    })
  },


  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  //验证函数
  initValidate() {
    const rules = {
      phone: {
        required: true,
        tel: true
      },
      code: {
        required: true,
        number: true,
      }
    }
    const messages = {
      phone: {
        required:'手机号码不能为空!',
        tel:'请填写正确的手机号'
      },
      code: {
        required:'验证码不能为空!',
        number: '验证码格式错误',
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  //调用验证函数
  formSubmit: function(e) {
    let _self = this;
    // let urlBase = app.globalData.urlBase
    let openid = wx.getStorageSync('storeToken')
    let value = e.detail.value;
    let params = {
      phone: value.phone,
      smCode: value.code,
      openid: openid
    }
    //校验表单
    if (this.WxValidate.checkForm(value)) {
      http.httpPost('/wx/ajaxSMLogin', params, (res) => {
        if (res.code == 1) {
          if (res.isBindedTJ) {
            wx.setStorageSync('isBindedTJ', res.isBindedTJ)
          }
          _self.showModal({
            msg: '绑定糖家成功！'
          })
          wx.navigateBack()
        } else {
          _self.showModal({
            msg: res.msg
          })
        }
      })
    } else {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
    }
  },
  // 获取短信验证码
  getSMCode() {
    let _self = this;
    let phone = this.data.form.phone
    if (phone != null && phone != '') {
      http.httpPost('/wx/getSM', {phone: phone}, (res) => {
        if (res.state != -1) {
          // console.log(res);
          _self.setData({
            codeBtnText: '验证码已发送'
          })
          _self.minDownSet();
        } else {
          _self.showModal(res)
        }
      })
    } else {
      wx.showModal({
        content: '请输入正确的电话号码！',
        showCancel: false,
      })
      return
    }
  },
  minDownSet() {
    let time = 60;
    let _self = this;
    let func = setInterval(function() {
      if (time != 0) {
        time--;
        _self.setData({
          codeBtnText: time + '秒后重试',
          btnStatus: true
        })
      } else {
        clearInterval(func)
        _self.setData({
          codeBtnText: '获取验证码',
          btnStatus: false
        })
      }
    }, 1000)
  }

})